<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 视频评论 模型
 * @package app\user_center\model
 */
class VideoPlayComment extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_video_play_comment';
	
	use SoftDelete;
    protected $deleteTime = 'comment_status';
    protected $defaultSoftDelete = 0;

    public static function init()
    {
        self::event('after_insert', function ($user) {
            //评论数增加
            model('VideoPlay') -> incCommentTimes($user->play_id);
        });
        self::event('after_delete', function ($user) {
            //评论数减少
            model('VideoPlay') -> decCommentTimes($user->play_id);
        });
    }

    /**
     * 添加数据
     * @param int $uid 用户id
     * @param int $play_id 视频id
     * @param int $content 评论内容
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function addData($uid, $play_id, $content, $create_time = ''){
        $video_info = model('video/VideoPlay') -> getPlayFatherPlayInfo($play_id);
		$play_id = $video_info['data'];//关联的视频id	
		
		//视频作者id
		$play_authorid = model('video/VideoPlay') -> cache(true) -> find($play_id) -> uid;

        $data['uid']			= $uid;
        $data['play_id']		= $play_id;
        $data['content']		= $content;
        $data['play_authorid']  = $play_authorid;
        if($create_time != '') $data['create_time']	= $create_time;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this -> id);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 添加数据
     * @param int $uid 用户id
     * @param int $play_id 视频id
     * @param int $content 评论内容
     * @param int $create_time 评论时间
     * @author shiqiang <120907037@qq.com>
     * @return bool
     */
    /* public function addDataTimes($uid, $play_id, $content, $create_time){
        $data['uid']			= $uid;
        $data['play_id']		= $play_id;
        $data['content']		= $content;
        $data['create_time']	= $create_time;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    } */

    /**
     * 删除数据
     * @param int $comment_id 评论id
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function deleteData($comment_id){

        $res = self::destroy($comment_id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 点赞数增加
     * @param int $comment_id 评论id
     * @param int $times 数量（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function incSupportTimes($comment_id, $times = 1){
        $map = [];
        $map['id'] = $comment_id;
        $res = $this -> where($map) -> setInc('support_times', $times);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 点赞数减少
     * @param int $comment_id 评论id
     * @param int $times 数量（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function decSupportTimes($comment_id, $times = 1){
        $map = [];
        $map['id'] = $comment_id;
        $res = $this -> where($map) -> setDec('support_times', $times);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 回复数增加
     * @param int $comment_id 评论id
     * @param int $times 数量（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function incReplyTimes($comment_id, $times = 1){
        $map = [];
        $map['id'] = $comment_id;
        $res = $this -> where($map) -> setInc('reply_times', $times);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 回复数减少
     * @param int $comment_id 评论id
     * @param int $times 数量（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function decReplyTimes($comment_id, $times = 1){
        $map = [];
        $map['id'] = $comment_id;
        $res = $this -> where($map) -> setDec('reply_times', $times);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 获取视频评论列表
     * @param int $play_id 视频id
     * @param int $page 页码（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function getListByPlayid($play_id, $page = 1){
        $map = [];
        $map['play_id'] = $play_id;
        $list = $this -> withJoin('getUcMember', 'LEFT') -> where($map) -> order('support_times desc,create_time desc') -> paginate();
        return array_out(200, 'success', $list);
    }

    /**
     * 获取单条数据
     * @param int $play_id 视频id
     * @param int $page 页码（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function getDataByCommentid($commentid, $cache = false){
        $map = [];
        $info = $this -> where($map) -> cache($cache) -> find($commentid);
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 用户信息 关联载入（一对一）
     */
    public function getUcMember(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'uid');
    }

    /**
     * 视频表 关联载入（一对一）
     */
    public function getVideoPlay(){
        return $this -> hasOne('app\video\model\VideoPlay','id', 'play_id');
    }
}
